class Dizhen::EventsController < Dizhen::BaseController
  before_filter :unread_messages_amount
  
  def index
    build_events(["events.end_at > now() and events.ref='dizhen.1kg.org'"])
  end
  
  def all
    build_events(["events.ref='dizhen.1kg.org'"])
  end
  
  def category
    build_events(["events.category=? and events.ref='dizhen.1kg.org'", params[:cat].to_i])
  end
  
  private
  def build_events(conditions = "1=1")
    @events = Event.paginate(:page => params[:page] || 1,
                             :conditions => conditions,
                             :order => "events.created_at desc",
                             :include => [:submitor, :school, :pic])
    @categories_count = Event.find_by_sql("select category, count(*) as count from events where ref='dizhen.1kg.org' group by category")
    @participations = Participation.find(:all, :conditions => "events.ref='dizhen.1kg.org'",
                                               :order => "participations.created_at desc", 
                                               :limit => 10,
                                               :include => [:user, :event])
  end

end